export function blockSensitiveInfo<T>(data: T, keys?: string[]): T {
  keys = keys ?? ['password'];
  if (Array.isArray(data)) {
    data.forEach((item: T) => {
      keys.forEach((key) => {
        item.hasOwnProperty(key) && delete item[key];
      });
    });
  } else {
    keys.forEach((key) => {
      data.hasOwnProperty(key) && delete data[key];
    });
  }
  return data;
}

export function parseRecord(records: any, keys?: string[]) {
  keys = keys || ['tags', 'images', 'roles', 'role'];
  if (Array.isArray(records)) {
    records.forEach((record) => {
      for (const key of keys) {
        if (record.hasOwnProperty(key)) {
          try {
            record[key] = JSON.parse(record[key]);
          } catch (error) {
            console.log('parseRecord', parseRecord);
          }
        }
      }
    });
  } else {
    for (const key of keys) {
      if (records.hasOwnProperty(key)) {
        try {
          records[key] = JSON.parse(records[key]);
        } catch (error) {
          console.log('parseRecord', parseRecord);
        }
      }
    }
  }

  return records;
}

export function stringifyRecord(records: any, keys?: string[]) {
  keys = keys || ['tags', 'images', 'roles', 'role'];
  if (Array.isArray(records)) {
    records.forEach((record) => {
      for (const key of keys) {
        if (record.hasOwnProperty(key)) {
          record[key] = JSON.stringify(record[key]);
        }
      }
    });
  } else {
    for (const key of keys) {
      if (records.hasOwnProperty(key)) {
        records[key] = JSON.stringify(records[key]);
      }
    }
  }

  return records;
}
